home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / gnu / progutil / stdwin.zoo / tools / strdup.c < prev    next >
C/C++ Source or Header  |  1990-03-29  |  964b  |  68 lines

  1. /* Save a copy of a string. */
  2.  
  3. #include "configure.h"
  4.  
  5. #ifdef LSC
  6.  
  7. /* Function prototypes */
  8.  
  9. #include <proto.h>
  10. char *strdup(char *str);
  11. char *strndup(char *str, int n);
  12.  
  13. #else
  14.  
  15.  
  16. #ifdef __STDC__
  17. #include <stddef.h>
  18. #include <string.h>
  19. #include <memory.h>
  20.  
  21. #ifndef NULL
  22. #define NULL ((void *)0)
  23. #endif
  24. void *malloc(size_t);
  25. char *strcpy(char *, const char *), *strncpy(char *, const char *, size_t);
  26. #else
  27. #define NULL 0
  28. char *malloc(), *strcpy(), *strncpy();
  29. #endif
  30.  
  31. #endif
  32.  
  33. #if !defined(atarist) && !defined(__GNUC__)
  34. char *
  35. strdup(str)
  36. #ifdef __STDC__
  37. const
  38. #endif
  39.     char *str;
  40. {
  41.     char *copy= NULL;
  42.     
  43.     if (str != NULL) {
  44.         copy= malloc((size_t) (strlen(str) + 1));
  45.         if (copy != NULL)
  46.             strcpy(copy, str);
  47.     }
  48.     return copy;
  49. }
  50. #endif
  51.  
  52. char *
  53. strndup(str, len)
  54.     char *str;
  55.     int len;
  56. {
  57.     char *copy= NULL;
  58.     
  59.     if (str != NULL) {
  60.         copy= malloc((size_t) (len + 1));
  61.         if (copy != NULL) {
  62.             strncpy(copy, str, (size_t)len);
  63.             copy[len]= '\0';
  64.         }
  65.     }
  66.     return copy;
  67. }
  68.